package com.example.testsvn;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class BinhBau extends Activity {
	public final static String EXTRA_ID = "ID";


	private Button btnDanhGia;

	private RadioGroup radioGroupThaiDo;
	private RadioGroup radioGroupNghiepVu;
	private RadioGroup radioGroupMinhBach;
	private RadioGroup radioGroupDungHen;

	private int mThaiDo;
	private int mNghiepVu;
	private int mDungHen;
	private int mMinhBach;

	// boolean that controls if the Thread will run or not
	private boolean activeThread = true;
	private int timeCounter = 100;
	private TextView threadModifiedText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_binh_bau);
		Intent intent1 = getIntent();
		final String ID = intent1.getStringExtra(TrangChaoActivity.EXTRA_ID);
		final String SBN = intent1.getStringExtra(TrangChaoActivity.EXTRA_SBN);

		btnDanhGia = (Button) findViewById(R.id.btnDanhGia);
		radioGroupThaiDo = (RadioGroup) findViewById(R.id.radioGroupThaiDo);
		radioGroupNghiepVu = (RadioGroup) findViewById(R.id.radioGroupNghiepVu);
		radioGroupDungHen = (RadioGroup) findViewById(R.id.radioGroupDungHen);
		radioGroupMinhBach = (RadioGroup) findViewById(R.id.radioGroupMinhBach);
		threadModifiedText = (TextView) findViewById(R.id.counter);

		radioGroupThaiDo
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						timeCounter = 100;
					}
				});

		radioGroupNghiepVu
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						timeCounter = 100;
					}
				});

		radioGroupDungHen
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						timeCounter = 100;
					}
				});

		radioGroupMinhBach
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						timeCounter = 100;
					}
				});

		btnDanhGia.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				timeCounter = 100;
				mThaiDo = radioGroupThaiDo.getCheckedRadioButtonId();
				mNghiepVu = radioGroupNghiepVu.getCheckedRadioButtonId();
				mDungHen = radioGroupDungHen.getCheckedRadioButtonId();
				mMinhBach = radioGroupMinhBach.getCheckedRadioButtonId();

				if (mThaiDo < 0 || mNghiepVu < 0 || mDungHen < 0 || mMinhBach < 0) {
					Context context = getApplicationContext();
					CharSequence text = "Bạn chưa chọn đủ các tiêu chí";
					int duration = Toast.LENGTH_SHORT;
					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
					return;
				}

				if (mThaiDo == R.id.radioGroupThaiDo_cancel)
					mThaiDo = 0;
				else if (mThaiDo == R.id.radioGroupThaiDo_ok)
					mThaiDo = 1;

				if (mNghiepVu == R.id.radioGroupNghiepVu_cancel)
					mNghiepVu = 0;
				else if (mNghiepVu == R.id.radioGroupNghiepVu_ok)
					mNghiepVu = 1;
				
				if (mDungHen == R.id.radioGroupDungHen_cancel)
					mDungHen = 0;
				else if (mDungHen == R.id.radioGroupDungHen_ok)
					mDungHen = 1;
				
				if (mMinhBach == R.id.radioGroupMinhBach_cancel)
					mMinhBach = 0;
				else if (mMinhBach == R.id.radioGroupMinhBach_ok)
					mMinhBach = 1;
				
				// Creating JSON Parser object
				JSONParser jParser = new JSONParser();
				// url to get all products list
				String url_xlLogin = "http://10.86.80.217/DoAnThucTap1/xlDanhGia.php";
				int success;
				
				try {
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					params.add(new BasicNameValuePair("id", ID));
					params.add(new BasicNameValuePair("sbn", SBN));
					params.add(new BasicNameValuePair("thaido", Integer.toString(mThaiDo)));
					params.add(new BasicNameValuePair("nghiepvu", Integer.toString(mNghiepVu)));
					params.add(new BasicNameValuePair("dunghen", Integer.toString(mDungHen)));
					params.add(new BasicNameValuePair("minhbach", Integer.toString(mMinhBach)));
					JSONObject json = jParser.makeHttpRequest(url_xlLogin,
							"POST", params);
					// check your log for json response
					Log.d("Login", json.toString());
					success = json.getInt("success");
					if (success == 1){
						Intent intent = new Intent(getApplicationContext(),TrangChaoActivity.class);
						intent.putExtra(EXTRA_ID, ID);
						startActivity(intent);
					}
					else {
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					
				
				// Xử lý chuyển activity ở đây
				
			}

		});

		// Thread for count-down
		Thread myTread = new Thread() {
			@Override
			public void run() {
				try {
					while (activeThread && (timeCounter > 0)) {
						sleep(100);
						// once per second notifies the Handler to update the
						// displayed count-down
						if (timeCounter % 10 == 0) {
							threadHandler
									.sendEmptyMessage((int) timeCounter / 10);
						}
						timeCounter--;
					}
				} catch (InterruptedException e) {
					// don't forget to deal with exceptions....
				} finally {
					// this forces the activity to end (also the application in
					// our case)
					
					// Xử lý chuyển activity ở đây
					finish();
				}
			}
		};

		// starting thread
		myTread.start();

	}

	// manages user touching the screen
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			// we set the activeThread boolean to false, forcing the Thread to
			// stop
			activeThread = true;
			timeCounter = 100;
		}
		return super.onTouchEvent(event);
	}

	private Handler threadHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			// whenever the Thread notifies this handler we have
			// only this behavior
			threadModifiedText.setText(Integer.toString(msg.what) + "s");
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_binh_bau, menu);
		return true;
	}
}
